local gongzheng = fk.CreateSkill {
  name = "wk_heg__gongzheng",
  tags = {Skill.DeputyPlace},
}

Fk:loadTranslationTable{
  ["wk_heg__gongzheng"] = "躬正",
  [":wk_heg__gongzheng"] = "副将技，当你的装备牌因弃置而置入弃牌堆后，你可以将这些牌当【桃】对一名其他角色使用，然后你交换主副将。",

  ["#wk_heg__gongzheng_use"] = "躬正：你可以选择一名其他角色，将这些牌当【桃】对其使用",
}

local HG = require "packages/new_heg_diy_my/util"

gongzheng:addEffect(fk.AfterCardsMove,{
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(gongzheng.name) then
      local room = player.room
      local cards = {}
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile and move.from and move.from == player and not move.from.dead then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if Fk:getCardById(info.cardId).type == Card.TypeEquip then
                table.insert(cards, info.cardId)
                cards = room.logic:moveCardsHoldingAreaCheck(cards)
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("peach")
    local cards = event:getCostData(self).cards
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player, false)) do
      if player:canUseTo(card, p) then
        table.insert(targets, p)
      end
    end
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#wk_heg__gongzheng_use",
      cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos[1], cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos
    room:useVirtualCard("peach", cards, player, to, gongzheng.name, true)
    HG.SwapMainAndDeputy(room, player)
  end,
})

return gongzheng